package com.boot.web.controller;

import java.util.List;
import java.util.Map;

import com.boot.web.dao.OrderDetailsDao;
import com.boot.web.dao.ProductDao;
import com.boot.web.dao.SkuRelationDao;
import com.boot.web.entity.Product;
import com.boot.web.entity.SkuRelation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.OrderDetails;
import com.boot.web.service.OrderDetailsService;

/**
 * 订单详情控制器
 * @author 【zhouJie】
 * @date 2021-06-05 14:56:01
 */
@RestController
@RequestMapping("/orderDetails")
public class OrderDetailsController {

    @Autowired
    private OrderDetailsService orderDetailsService;

    @PostMapping("/save")
    public ResponseServer save(OrderDetails orderDetails) {
        return ResponseServer.success("添加成功", orderDetailsService.save(orderDetails));
    }

    @PutMapping("/update")
    public ResponseServer update(OrderDetails orderDetails) {
        return ResponseServer.success("修改成功", orderDetailsService.update(orderDetails));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        return ResponseServer.success("删除成功", orderDetailsService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", orderDetailsService.batchDelete(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(orderDetailsService.getPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(orderDetailsService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(orderDetailsService.getById(id));
    }

    @Autowired
    private OrderDetailsDao orderDetailsDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private SkuRelationDao skuRelationDao;

    /**
     * 给以前的订单set产品编号和成本价
     * @return
     */
    @GetMapping("/updateOrderDetailsByCodeAndPrice")
    public ResponseServer updateOrderDetailsByCodeAndPrice() {

        List<OrderDetails> odList = orderDetailsDao.queryAll();
        if(odList.size() > 0){
            for(OrderDetails od : odList){
                Product product = productDao.queryById(od.getProductId());
                if(product != null){
                    od.setProductCode(product.getProductCode());
                }
                SkuRelation sku = skuRelationDao.queryById(od.getRelationId());
                if(sku != null){
                    od.setCostPrice(sku.getCostPrice());
                }
                orderDetailsDao.update(od);
            }
        }

        return ResponseServer.success();
    }

}
